def sim(samplesize, dbname, dbtype, alleletype):
    from subprocess import call
    from load_sim import generate_sim, load_sim
    from make_output import make_output
    from init_schema import init_schema
    import sys
    saveout = sys.stdout
    f = open('log.txt', 'a')
    sys.stdout = f
    sys.stderr = f
    #call("python load_sim.py 10 -y illumina_faheem illumina -a forward", shell=False)
    #call("python load_sim.py 10 -y illumina_faheem illumina -a forward", shell=True)
    #call("python load_sim.py 10 -y illumina_faheem illumina -a forward", shell=False)
    #call("python load_sim.py 10 -sy illumina_faheem illumina -a forward", shell=False)
    #call("python make_output.py -y ped sim10 illumina_faheem -a forward", shell=False)
    #call("python make_output.py -sy ped sim10 illumina_faheem -a forward", shell=False)

    import datetime
    now = datetime.datetime.now()

    print '************************************************'
    print "starting run at %s"%now.strftime("%Y-%m-%d %H:%M")
    print '************************************************'
    f.flush()

    # generate simulation data
    print 'STARTING generate_sim with %s'%({'samplesize':samplesize, 'dbtype':dbtype})
    f.flush()
    generate_sim(samplesize, dbtype)
    print 'ENDING generate_sim with %s'%({'samplesize':samplesize, 'dbtype':dbtype})
    f.flush()

    dataset = "sim"+str(samplesize)

    # load dataset - Geno Single version
    print 'STARTING load_sim with %s'%({'samplesize':samplesize, 'dbname':dbname, 'dbtype':dbtype, 'alleletype':alleletype, 'jobs':1, 'recreate':True, 'shard':False})
    f.flush()
    from sqlalchemy.orm import clear_mappers
    clear_mappers()
    load_sim(samplesize, dbname, dbtype, alleletype, jobs=1, recreate=True, shard=False)
    print 'ENDING load_sim with %s'%({'samplesize':samplesize, 'dbname':dbname, 'dbtype':dbtype, 'alleletype':alleletype, 'jobs':1, 'recreate':True, 'shard':False})
    f.flush()

    # load dataset - Geno Shard version
    print 'STARTING load_sim with %s'%({'samplesize':samplesize, 'dbname':dbname, 'dbtype':dbtype, 'alleletype':alleletype, 'jobs':4, 'recreate':True, 'shard':True})
    f.flush()
    from sqlalchemy.orm import clear_mappers
    clear_mappers()
    load_sim(samplesize, dbname, dbtype, alleletype, jobs=4, recreate=True, shard=True)
    print 'ENDING load_sim with %s'%({'samplesize':samplesize, 'dbname':dbname, 'dbtype':dbtype, 'alleletype':alleletype, 'jobs':4, 'recreate':True, 'shard':True})
    f.flush()

    # make PED file output - Geno Single version
    for j in 1, 2, 4:
        print 'STARTING make_output with %s'%({'fileformat':'ped', 'datasets':[dataset], 'dbname':dbname, 'alleletype':alleletype, 'mapcol':False, 'destdir':'.', 'jobs':j, 'shard':False, 'test':False, 'recreate':True })
        f.flush()
        make_output(fileformat='ped',  datasets=[dataset], dbname=dbname, alleletype=alleletype, mapcol=False, destdir='.', jobs=j, shard=False, test=False, recreate=True)
        print 'ENDING make_output with %s'%({'fileformat':'ped', 'datasets':[dataset], 'dbname':dbname, 'alleletype':alleletype, 'mapcol':False, 'destdir':'.', 'jobs':j, 'shard':False, 'test':False, 'recreate':True})
        f.flush()

    # make PED file output - Geno Shard version
    for j in 1, 2, 4:
        print 'STARTING make_output with %s'%({'fileformat':'ped', 'datasets':[dataset], 'dbname':dbname, 'alleletype':alleletype, 'mapcol':False, 'destdir':'.', 'jobs':j, 'shard':True, 'test':False, 'recreate':True})
        f.flush()
        make_output(fileformat='ped', datasets=[dataset], dbname=dbname, alleletype=alleletype, mapcol=False, destdir='.', jobs=j, shard=True, test=False, recreate=True)
        print 'ENDING make_output with %s'%({'fileformat':'ped', 'datasets':[dataset], 'dbname':dbname, 'alleletype':alleletype, 'mapcol':False, 'destdir':'.', 'jobs':j, 'shard':True, 'test':False, 'recreate':True})
        f.flush()

    # remove dataset schema
    init_schema(dataset, dbname, recreate=True)
    init_schema(dataset+'_shard', dbname, recreate=True)

    now = datetime.datetime.now()
    print '************************************************'
    print "ending run at %s"%now.strftime("%Y-%m-%d %H:%M")
    print '************************************************'
    f.flush()

    sys.stdout = sys.__stdout__
    sys.stdout = sys.__stderr__

dbname = 'illumina_faheem'
for samplesize in 10,:
    sim(samplesize=samplesize, dbname=dbname, dbtype='illumina', alleletype='forward')

